package hongvt.quanlynhanvien.adapter;

import hongvt.quanlynhanvien.R;
import hongvt.quanlynhanvien.model.Employee;
import hongvt.quanlynhanvien.model.WorkingMode;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ListEmpAdapter extends BaseAdapter {
	Context context;
	List<Employee> listEmployees;
	List<WorkingMode> listWorkingModes;

	public ListEmpAdapter(Context context, List<Employee> listEmployees,
			List<WorkingMode> listWorkingModes) {
		this.context = context;
		this.listEmployees = listEmployees;
		this.listWorkingModes = listWorkingModes;
	}

	@Override
	public int getCount() {
		if (listEmployees != null) {
			return listEmployees.size();
		}

		return 0;
	}

	@Override
	public Employee getItem(int position) {
		// TODO Auto-generated method stub
		return listEmployees.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {

			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			convertView = inflater.inflate(R.layout.layout_item_list_emp, null,
					false);
		}

		ImageView avaIv = (ImageView) convertView.findViewById(R.id.iv_ava);
		TextView nameTv = (TextView) convertView.findViewById(R.id.tv_name);
		TextView workingModeTv = (TextView) convertView
				.findViewById(R.id.tv_working_type);

		Employee emp = getItem(position);
		nameTv.setText(emp.getName());
		if (emp.getGender() == 0) {
			avaIv.setImageResource(R.drawable.male);
		} else {
			avaIv.setImageResource(R.drawable.female);
		}

		if (listWorkingModes != null) {
			for (WorkingMode wm : listWorkingModes) {
				if (wm.getEmpId()
						.equals(emp.getEmpId())) {
					if (wm.getWorkingMode() == 0) {
						workingModeTv.setText(R.string.fulltime);
					} else {
						workingModeTv.setText(R.string.parttime);
					}

					break;
				}
			}
		}

		return convertView;
	}

}
